<template>
    <div class="goodsinfo-container">

        <transition
            v-on:before-enter="beforeEnter"
            v-on:enter="enter"
            v-on:after-enter="afterEnter"
        >
            <div class="ball" v-show="flag" ref="ball"></div>
        </transition>

        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotuList="lunbotuList" :isFull="false"></swiper>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">{{ goodsInfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>
                        市场价：<span class="market" >￥{{ goodsInfo.market_price }}</span>&nbsp;&nbsp;
                        销售价：<span class="sell">￥{{ goodsInfo.sell_price }}</span>
                    </p>
                    <p>
                        购买数量：
                        <numberbox @getCount="getSelectedCount"  :max="goodsInfo.stock_quantity"></numberbox>
                    </p>
                    <mt-button type="primary">立即购买</mt-button>
                    <mt-button type="danger" v-on:click="addToShopCar">加入购物车</mt-button>
                </div>
            </div>
        </div>


        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品编号：{{ goodsInfo.goods_no }}</p>
                    <p>库存情况：{{ goodsInfo.stock_quantity }}</p>
                    <p>上架时间：{{ goodsInfo.add_time }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goodsDesc(goodsInfo.id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goodsComment">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    // 导入轮播插件
    import swiper from '../subcomponents/swiper.vue'
    // 导入numberbox
    import goodsinfoBox from '../subcomponents/goodsinfo_numberbox.vue'

    export default {
        data () {
            return {
                id: this.$route.params.id,
                lunbotuList: [],
                goodsInfo: {},
                flag: false,
                selectedCount: 1 // 用户购买的数量，默认为 1
            }
        },
        created() {
          this.getGoodsPhotoList();
          this.getGoodsInfo();
        },
        methods: {
            getGoodsPhotoList() {
                this.$http.get('./home/PhotoList.json?id=' + this.id).then( result => {
                   if (result.body.status === 0) {
                       result.body.message.forEach( item => {
                           item.img = item.img_url;
                       });
                       this.lunbotuList = result.body.message;
                   }
                });
            },
            getGoodsInfo() {
                this.$http.get('./home/GoodsInfo.json?id=' + this.id).then( result => {
                    if (result.body.status === 0) {
                        this.goodsInfo = result.body.message[0];
                    }
                });
            },
            goodsDesc(id) {
                this.$router.push({ name: 'goodsdesc', params: {id}});
            },
            goodsComment() {
                this.$router.push({ name: 'goodscomment', params: {id: this.id}});
            },
            addToShopCar() {
                // 显示或隐藏小球
                this.flag = !this.flag;
                // 拼接对象
                var goodsinfo = {
                    id: this.id,
                    count: this.selectedCount,
                    price: this.goodsInfo.sell_price,
                    selected: true
                }
                // 保存到 store 中
                this.$store.commit('addToCar', goodsinfo);
            },
            // 动画效果
            beforeEnter(el) {
                el.style.transform= 'translate(0, 0)';
            },
            enter(el, done) {
                el.offsetWidth;
                // 根据不同分辨率和位移的不同位置，不能固定位移的距离
                // 获取 小球 的坐标
                var ballPosition = this.$refs.ball.getBoundingClientRect();
                // 获取 徽标 的坐标
                var badgePosition = document.getElementById('badge').getBoundingClientRect();
                // 计算 X轴 间距
                var xDist = badgePosition.left - ballPosition.left;
                // 计算 Y轴 间距
                var yDist = badgePosition.top - ballPosition.top;

                el.style.transform = `translate(${xDist}px, ${yDist}px)`;
                // 使用bezier作为动画效果
                el.style.transition = 'all 0.5s cubic-bezier(0.4, -0.3, 1, 0.68';
                done()
            },
            afterEnter(el) {
                this.flag = !this.flag;
            },
            getSelectedCount(count) {
                // 获取子组件传来的 count
                this.selectedCount = count;
                console.log('p:' + this.selectedCount);
            }
        },
        components: {
            swiper,
            'numberbox': goodsinfoBox
        }
    }
</script>

<style lang="scss" scoped>
.goodsinfo-container {
    background-color: #eeeeee;
    overflow: hidden;

    .market {
        text-decoration: line-through;
        font-size: 12px;
    }
    .sell {
        color: red;
        font-size: 14px;
    }
    .mui-card-footer {
        display: block;
        button {
            margin: 15px 0;
        }
    }

    .ball {
        width: 15px;
        height: 15px;
        background-color: red;
        z-index: 99;
        border-radius: 50%;
        position: absolute;
        top: 390px;
        left: 150px;
    }
}
</style>
